package com.springboot3login.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.springboot3login.utils.JwtUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author mijiupro
 */
 
 
@RestController
@RequestMapping("/test")
public class TestController {
    private final JwtUtils jwtUtils;
    public TestController(JwtUtils jwtUtils) {
        this.jwtUtils = jwtUtils;
    }
 
    @GetMapping("/get-token")
    public String getToken() {
        Map<String, Object> claims = new HashMap<>();
        claims.put("username", "mijiu");
        claims.put("role", "admin");
        return jwtUtils.generateToken(claims,"user");
    }
    @GetMapping("/parse-token/{token}")
    public  String parseToken(@PathVariable String token) {
        return jwtUtils.parseToken(token).toString();
 
    }

    @GetMapping("/build-tree")
    public List<Tree<String>> buildTree() {
        // 构建node列表
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();

        nodeList.add(new TreeNode<>("1", "0", "系统管理", null));
        nodeList.add(new TreeNode<>("11", "1", "用户管理", null));
        nodeList.add(new TreeNode<>("111", "11", "用户添加", 0));
        nodeList.add(new TreeNode<>("2", "0", "店铺管理", null));
        nodeList.add(new TreeNode<>("21", "2", "商品管理", 44));
        nodeList.add(new TreeNode<>("221", "2", "商品管理2", 2));
        nodeList.add(new TreeNode<>("2221", "221", "图书管理", 2));
        nodeList.add(new TreeNode<>("22221", "2221", "图书管理1", null));

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
      /*  // 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("order");
        treeNodeConfig.setIdKey("rid");*/
        // 最大递归深度
//        treeNodeConfig.setDeep(3);

        //转换器
       return  TreeUtil.build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                });
    }
}